import request from "../utils/request";
import md5 from "md5";

const secret = '123jiasW';
class Login {
  static login = async ({ tel, password }) => {
    let res = await request.get("/admin", { params: { tel ,password:md5(password+secret)} })
    if(res.data.length === 0){
      return {data:{message:"账号或密码错误"},code:111}
    }else{
      return {data:{data:res.data ,message:"登录成功",code:200},code:200}
    }
  }
  // 登录接口
  static getadminlist = async () => {
    let res = await request.get('/admin')
    return { data: { message: "获取管理员列表成功", data: res.data }, code: 200 }
  }
  // 获取管理员列表接口
  static register = async (data) => {
    let { adminid, tel } = data
    if (tel.length < 11) {
      return { data: { message: "请输入正确手机号" }, code: 111 }
    } else {
      let res = await request.get("/admin", { params: { tel } })
      if (res.data.length === 0) {
        return { data: { message: "手机号未注册", data: res }, code: 200 }
      } else {
        return { data: { message: "手机号已注册" }, code: 111 }
      }
    }


  }
  static doRegister = async (data) => {
    let res = await request.post("/admin", {...data,password:md5(data.password+secret)})
    return { data: { data: res.data, message: "注册成功" }, code: 200 }
  }
  // 注册接口
  static deladmin = async(id)=>{
    let res = await request.delete("/admin/"+id)
    console.log(res);
    return { data: { message: "删除管理员成功" }, code: 200 }
  }
  // 删除管理员接口
  static searchadmin = async(adminid)=>{
    let res = await request.get("/admin", { params: { adminid} })
    console.log(res);
    return { data: { message: "获取管理员信息成功" ,data:res.data}, code: 200 }
  }
  // 获取管理员详细信息接口
  static changePermissions = async(data)=>{
    let{id,checkedKeys} = data
    let res = await request.patch("/admin/"+id,  {checkedKeys} )
    return { data: { message: "管理员权限修改成功",data:res.data }, code: 200 }
  }
  //修改管理员权限接口
  static changepassword = async(data)=>{
    let{id,password} = data
    let res = await request.patch("/admin/"+id,  {password:md5(password+secret)} )
    return { data: { message: "管理员密码修改成功",data:res.data }, code: 200 }
}
// 管理员密码修改接口
}
export default Login